﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace GapCC.BritishGas
{
    public class Habits
    {
        public Habits() { }

        private string _SchoolId;
        public string SchoolId
        {
            get { return _SchoolId; }
            set { _SchoolId = value; }
        }

        private string _ClassName;
        public string ClassName
        {
            get { return _ClassName; }
            set { _ClassName = value; }
        }

        private DateTime _Start;
        public DateTime Start
        {
            get { return _Start; }
            set { _Start = value; }
        }

        private DateTime _End;
        public DateTime End
        {
            get { return _End; }
            set { _End = value; }
        }

        private int? _LightBulbsTotal;
        public int? LightBulbsTotal
        {
            get { return _LightBulbsTotal; }
            set { _LightBulbsTotal = value; }
        }

        private int? _LightBulbsLeftOn;
        public int? LightBulbsLeftOn
        {
            get { return _LightBulbsLeftOn; }
            set { _LightBulbsLeftOn = value; }
        }

        private int? _MonitorsTotal;
        public int? MonitorsTotal
        {
            get { return _MonitorsTotal; }
            set { _MonitorsTotal = value; }
        }

        private int? _MonitorsLeftOn;
        public int? MonitorsLeftOn
        {
            get { return _MonitorsLeftOn; }
            set { _MonitorsLeftOn = value; }
        }

        private int? _ComputersTotal;
        public int? ComputersTotal
        {
            get { return _ComputersTotal; }
            set { _ComputersTotal = value; }
        }

        private int? _ComputersLeftOn;
        public int? ComputersLeftOn
        {
            get { return _ComputersLeftOn; }
            set { _ComputersLeftOn = value; }
        }

        private int? _PrintersTotal;
        public int? PrintersTotal
        {
            get { return _PrintersTotal; }
            set { _PrintersTotal = value; }
        }

        private int? _PrintersLeftOn;
        public int? PrintersLeftOn
        {
            get { return _PrintersLeftOn; }
            set { _PrintersLeftOn = value; }
        }

        private int? _WhiteBoardsTotal;
        public int? WhiteBoardsTotal
        {
            get { return _WhiteBoardsTotal; }
            set { _WhiteBoardsTotal = value; }
        }

        private int? _WhiteBoardsLeftOn;
        public int? WhiteBoardsLeftOn
        {
            get { return _WhiteBoardsLeftOn; }
            set { _WhiteBoardsLeftOn = value; }
        }

        private int? _DeskLampsTotal;
        public int? DeskLampsTotal
        {
            get { return _DeskLampsTotal; }
            set { _DeskLampsTotal = value; }
        }

        private int? _DeskLampsLeftOn;
        public int? DeskLampsLeftOn
        {
            get { return _DeskLampsLeftOn; }
            set { _DeskLampsLeftOn = value; }
        }

        private int? _TelevisionsTotal;
        public int? TelevisionsTotal
        {
            get { return _TelevisionsTotal; }
            set { _TelevisionsTotal = value; }
        }

        private int? _TelevisionsLeftOn;
        public int? TelevisionsLeftOn
        {
            get { return _TelevisionsLeftOn; }
            set { _TelevisionsLeftOn = value; }
        }

        private int? _DVDsTotal;
        public int? DVDsTotal
        {
            get { return _DVDsTotal; }
            set { _DVDsTotal = value; }
        }

        private int? _DVDsLeftOn;
        public int? DVDsLeftOn
        {
            get { return _DVDsLeftOn; }
            set { _DVDsLeftOn = value; }
        }

        private float? _SickRoomAverage;
        public float? SickRoomAverage
        {
            get { return _SickRoomAverage; }
            set { _SickRoomAverage = value; }
        }

        private float? _ClassRoomAverage;
        public float? ClassRoomAverage
        {
            get { return _ClassRoomAverage; }
            set { _ClassRoomAverage = value; }
        }

        private float? _HallsAverage;
        public float? HallsAverage
        {
            get { return _HallsAverage; }
            set { _HallsAverage = value; }
        }

        private float? _WaterAverage;
        public float? WaterAverage
        {
            get { return _WaterAverage; }
            set { _WaterAverage = value; }
        }

        private bool _HeatingOn;
        public bool HeatingOn
        {
            get { return _HeatingOn; }
            set { _HeatingOn = value; }
        }

        private int? _WindowsTotal;
        public int? WindowsTotal
        {
            get { return _WindowsTotal; }
            set { _WindowsTotal = value; }
        }

        private int? _WindowsOpen;
        public int? WindowsOpen
        {
            get { return _WindowsOpen; }
            set { _WindowsOpen = value; }
        }

        private int? _InternalDoorsTotal;
        public int? InternalDoorsTotal
        {
            get { return _InternalDoorsTotal; }
            set { _InternalDoorsTotal = value; }
        }
        
        private int? _InternalDoorsOpen;
        public int? InternalDoorsOpen
        {
            get { return _InternalDoorsOpen; }
            set { _InternalDoorsOpen = value; }
        }

        private int? _ExternalDoorsTotal;
        public int? ExternalDoorsTotal
        {
            get { return _ExternalDoorsTotal; }
            set { _ExternalDoorsTotal = value; }
        }

        private int? _ExternalDoorsOpen;
        public int? ExternalDoorsOpen
        {
            get { return _ExternalDoorsOpen; }
            set { _ExternalDoorsOpen = value; }
        }

        private int? _RadiatorsTotal;
        public int? RadiatorsTotal
        {
            get { return _RadiatorsTotal; }
            set { _RadiatorsTotal = value; }
        }
        
        private int? _RadiatorsObstructed;
        public int? RadiatorsObstructed
        {
            get { return _RadiatorsObstructed; }
            set { _RadiatorsObstructed = value; }
        }

        internal bool Process()
        {
            Gap.Bll.BritishGas.Habits tmp = Gap.Bll.BritishGas.Habits.Get(this.SchoolId, this.ClassName, this.Start, this.End);
            if (tmp == null)
            {
                tmp = Gap.Bll.BritishGas.Habits.CreateInstance(this.SchoolId, this.ClassName, this.Start, this.End);
            }

            tmp.LightBulbsTotal = this.LightBulbsTotal;
            tmp.LightBulbsLeftOn = this.LightBulbsLeftOn;
            tmp.MonitorsTotal = this.MonitorsTotal;
            tmp.MonitorsLeftOn = this.MonitorsLeftOn;
            tmp.ComputersTotal = this.ComputersTotal;
            tmp.ComputersLeftOn = this.ComputersLeftOn;
            tmp.PrintersTotal = this.PrintersTotal;
            tmp.PrintersLeftOn = this.PrintersLeftOn;
            tmp.WhiteBoardsTotal = this.WhiteBoardsTotal;
            tmp.WhiteBoardsLeftOn = this.WhiteBoardsLeftOn;
            tmp.DeskLampsTotal = this.DeskLampsTotal;
            tmp.DeskLampsLeftOn = this.DeskLampsLeftOn;
            tmp.TelevisionsTotal = this.TelevisionsTotal;
            tmp.TelevisionsLeftOn = this.TelevisionsLeftOn;
            tmp.DVDsTotal = this.DVDsTotal;
            tmp.DVDsLeftOn = this.DVDsLeftOn;
            tmp.SickRoomAverage = this.SickRoomAverage;
            tmp.ClassRoomAverage = this.ClassRoomAverage;
            tmp.HallsAverage = this.HallsAverage;
            tmp.WaterAverage = this.WaterAverage;
            tmp.HeatingOn = this.HeatingOn;
            tmp.WindowsTotal = this.WindowsTotal;
            tmp.WindowsOpen = this.WindowsOpen;
            tmp.InternalDoorsTotal = this.InternalDoorsTotal;
            tmp.InternalDoorsOpen = this.InternalDoorsOpen;
            tmp.ExternalDoorsTotal = this.ExternalDoorsTotal;
            tmp.ExternalDoorsOpen = this.ExternalDoorsOpen;
            tmp.RadiatorsTotal = this.RadiatorsTotal;
            tmp.RadiatorsObstructed = this.RadiatorsObstructed;

            if (ConfigurationSettings.AppSettings["UseDB"] != "false")
            {
                tmp.Update();
            }
            return true;
        }

        public static Habits Load(string schoolId, string className, DateTime start, DateTime end)
        {
            Gap.Bll.BritishGas.Habits habits = null;
            if(ConfigurationSettings.AppSettings["UseDB"] != "false")
                habits = Gap.Bll.BritishGas.Habits.Get(schoolId, className, start, end);

            if (habits != null)
            {
                Habits tmp = new Habits();
                
                tmp.SchoolId = habits.SchoolId;
                tmp.ClassName = habits.ClassName;
                tmp.Start = habits.Start;
                tmp.End = habits.End;

                tmp.LightBulbsTotal = habits.LightBulbsTotal;
                tmp.LightBulbsLeftOn = habits.LightBulbsLeftOn;
                tmp.MonitorsTotal = habits.MonitorsTotal;
                tmp.MonitorsLeftOn = habits.MonitorsLeftOn;
                tmp.ComputersTotal = habits.ComputersTotal;
                tmp.ComputersLeftOn = habits.ComputersLeftOn;
                tmp.PrintersTotal = habits.PrintersTotal;
                tmp.PrintersLeftOn = habits.PrintersLeftOn;
                tmp.WhiteBoardsTotal = habits.WhiteBoardsTotal;
                tmp.WhiteBoardsLeftOn = habits.WhiteBoardsLeftOn;
                tmp.DeskLampsTotal = habits.DeskLampsTotal;
                tmp.DeskLampsLeftOn = habits.DeskLampsLeftOn;
                tmp.TelevisionsTotal = habits.TelevisionsTotal;
                tmp.TelevisionsLeftOn = habits.TelevisionsLeftOn;
                tmp.DVDsTotal = habits.DVDsTotal;
                tmp.DVDsLeftOn = habits.DVDsLeftOn;
                tmp.SickRoomAverage = habits.SickRoomAverage;
                tmp.ClassRoomAverage = habits.ClassRoomAverage;
                tmp.HallsAverage = habits.HallsAverage;
                tmp.WaterAverage = habits.WaterAverage;
                tmp.HeatingOn = habits.HeatingOn;
                tmp.WindowsTotal = habits.WindowsTotal;
                tmp.WindowsOpen = habits.WindowsOpen;
                tmp.InternalDoorsTotal = habits.InternalDoorsTotal;
                tmp.InternalDoorsOpen = habits.InternalDoorsOpen;
                tmp.ExternalDoorsTotal = habits.ExternalDoorsTotal;
                tmp.ExternalDoorsOpen = habits.ExternalDoorsOpen;
                tmp.RadiatorsTotal = habits.RadiatorsTotal;
                tmp.RadiatorsObstructed = habits.RadiatorsObstructed;
                
                return tmp;
            }
            else
            {
                return null;
            }
        }
    }
}
